package lifeimprint.BluetoothSample;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.UUID;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

public class BTGpsService extends Service{
	private BluetoothAdapter btadapter=null;
	private BluetoothSocket mysocket=null;
	private BluetoothDevice gpsdevices=null;
	private Handler mainhandler=null;
	private Mythread upthread=null;
	
	
	private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
	private static String gpsaddress = "00:02:76:C9:AE:AF";
	public static final String GPSINFOINFO="123";
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		Log.d("mydebug", "Server onBind");
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		btadapter=BluetoothAdapter.getDefaultAdapter();
		gpsdevices=btadapter.getRemoteDevice(gpsaddress);
		try {
			mysocket=gpsdevices.createRfcommSocketToServiceRecord(MY_UUID);
			mysocket.connect();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.d("mydebug", Thread.currentThread().getName()+":"+Thread.currentThread().getId());
		Log.d("mydebug", "Server onCreate");
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		upthread.ReceiverStopInfo("Yes");
		try {
			mysocket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.d("mydebug", "Server onDestroy");
		super.onDestroy();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		upthread=new Mythread();
		upthread.start();
		Log.d("mydebug", "Server onStartCommand");
		return super.onStartCommand(intent, flags, startId);
	}
	
	
	
	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		Log.d("mydebug", "Server onStart");
		super.onStart(intent, startId);
	}



	private class Mythread extends Thread{
		private InputStream is=null;
		private String line=null;
		private Bundle gpsbundle=null;
		private String Stop=null;
		
		public void ReceiverStopInfo(String sstop){
	    	Stop=sstop;
	    }

		@Override
		public void run() {
			// TODO Auto-generated method stub
			gpsbundle=new Bundle();
			Stop="no";
			Log.d("mydebug", Thread.currentThread().getName()+":"+Thread.currentThread().getId());
			Looper mainlooper=Looper.getMainLooper();
			Handler mainhandler=new Handler(mainlooper);
			mainhandler.removeMessages(0);
			try {
				is = mysocket.getInputStream();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			BufferedReader br=new BufferedReader(new InputStreamReader(is));
			try {
				while((line=br.readLine())!=null){
                        if(Stop.equals("no")){
						Log.d("mydebug", line);
						HandlingGPS hg=new HandlingGPS(line,"$GPRMC");
						gpsbundle=hg.outputgps();
						if(!gpsbundle.isEmpty()){
							/*
							Message m = mainhandler.obtainMessage();
							m.setData(gpsbundle);
							m.sendToTarget();*/
							Intent intent = new Intent(GPSINFOINFO);
							intent.putExtras(gpsbundle);
							sendBroadcast(intent);
						}
				     }
				}
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			super.run();
		}
		
	}
	
	

}
